<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\ding;

use app\api\model\ding\Food as GoodsModel;
use app\api\service\User as UserService;
use app\common\exception\BaseException;
use app\common\model\ding\Cart as CartModel;

/**
 * 购物车管理
 * Class Cart
 * @package app\api\model
 */
class Cart extends CartModel {

	public function getList(int $shopId = 0) {
		// 获取当前用户ID
		$userId = UserService::getCurrentLoginUserId();
		// 检索查询条件
		$filter = [];
		$filter[] = ['shop_id', '=', $shopId];
		$filter[] = ['user_id', '=', $userId];
		// 获取列表数据
		return $this->with(['food.image2'])

			->where($filter)
			->where('food_num', '>', 0)
			->where('is_delete', '=', 0)
			->order(['create_time' => 'desc'])
			->select()->toArray();
	}
	/**
	 * 加入购物车
	 * @param int $foodId 商品ID
	 * @param string $goodsSkuId 商品sku唯一标识
	 * @param int $goodsNum 商品数量
	 * @return bool
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function add(int $shopId, int $foodId, string $goodsSkuId, int $goodsNum) {
		// 判断是否已存在购物车记录
		$detail = $this->getInfo($foodId, $goodsSkuId, false);

		// 验证商品的状态
		$this->checkGoodsStatus($foodId, $goodsSkuId, $goodsNum);
		// 获取当前用户ID
		$userId = UserService::getCurrentLoginUserId();
		// 实例化模型
		$model = $detail ?: (new static );
		return $model->save([
			'food_id' => $foodId,
			'goods_sku_id' => $goodsSkuId,
			'food_num' => $goodsNum,
			'user_id' => $userId,
			'shop_id' => $shopId,
			'store_id' => self::$storeId,
		]);
	}

	/**
	 * 更新购物车记录
	 * @param int $foodId 商品ID
	 * @param string $goodsSkuId 商品sku唯一标识
	 * @param int $goodsNum 商品数量
	 * @return bool
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function sUpdate(int $foodId, string $goodsSkuId, int $goodsNum) {
		// 验证商品的状态
		$this->checkGoodsStatus($foodId, $goodsSkuId, $goodsNum);
		// 获取购物车记录
		$model = $this->getInfo($foodId, $goodsSkuId, true);
		// 更新记录
		return $model->save(['food_num' => $goodsNum]);
	}

	/**
	 * 验证商品的状态
	 * @param int $foodId 商品ID
	 * @param string $goodsSkuId 商品sku唯一标识
	 * @param int $goodsNum 商品数量
	 * @return bool
	 * @throws BaseException
	 */
	private function checkGoodsStatus(int $foodId, string $goodsSkuId, int $goodsNum) {
		// 获取商品详情
		$goods = GoodsModel::detail($foodId);
		// 商品不存在
		if (empty($goods) || $goods['is_delete']) {
			throwError('很抱歉, 商品信息不存在');
		}
		// 商品已下架
		// if ($goods['status'] == GoodsStatusEnum::OFF_SALE) {
		//     throwError('很抱歉, 该商品已经下架');
		// }

		return true;
	}

	/**
	 * 获取购物车记录
	 * @param int $foodId 商品ID
	 * @param string $goodsSkuId 商品sku唯一标识
	 * @param bool $isForce
	 * @return static|bool
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	private function getInfo(int $foodId, string $goodsSkuId, bool $isForce = true) {
		// 获取当前用户ID
		$userId = UserService::getCurrentLoginUserId();
		// 获取购物车记录
		$model = static::detail($userId, $foodId, $goodsSkuId);
		if (empty($model)) {
			$isForce && throwError('购物车中没有该记录');
			return false;
		}
		return $model;
	}

	/**
	 * 删除购物车中指定记录
	 * @param array $cartIds 购物车ID集, 如果为空删除所有
	 * @return bool
	 * @throws BaseException
	 */
	public function clear(array $cartIds = []) {
		// 获取当前用户ID
		$userId = UserService::getCurrentLoginUserId();
		// 设置更新条件
		$where = [['user_id', '=', $userId]];
		// 购物车ID集
		!empty($cartIds) && $where[] = ['id', 'in', $cartIds];
		// 更新记录
		return $this->updateBase(['is_delete' => 1], $where);
	}

	/**
	 * 获取当前用户购物车商品总数量
	 * @return float
	 * @throws BaseException
	 */
	public function getCartTotal() {
		if (!UserService::isLogin()) {
			return 0;
		}

		$userId = UserService::getCurrentLoginUserId();
		return $this->where('user_id', '=', $userId)
			->where('is_delete', '=', 0)
			->sum('food_num');
	}

}
